package com.example.photography.util;

import android.app.Activity;
import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.os.Environment;
import android.os.Looper;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.widget.PopupWindow;
import android.widget.Toast;

import androidx.fragment.app.FragmentActivity;

import com.bigkoo.pickerview.view.TimePickerView;
import com.example.photography.R;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by Mr.辉 on 2018/5/9.
 */

public class Util extends FragmentActivity {
	private static Toast toast;

	//所有的Toast必须使用该方法！
	//修复了多次点击导致Toast显示时间过长的BUG，用户体验感极好
	//因为toast的实现需要在activity的主线程才能正常工作，所以传统的非主线程不能使toast显示在actvity上，通过Handler可以使自定义线程运行于Ui主线程。
	public static void showToast(Context context, String content) {

		if (toast == null) {
			toast = Toast.makeText(context, content, Toast.LENGTH_SHORT);
		} else {
			toast.setText(content);
		}

		toast.show();

	}
	public void ClickLeft(View view) {
		finish();
	}


	public static void show(Context context, String text) {
		try {
			if(toast!=null){
				toast.setText(text);
			}else{
				toast= Toast.makeText(context, text, Toast.LENGTH_SHORT);
			}
			toast.show();
		} catch (Exception e) {
			//解决在子线程中调用Toast的异常情况处理
			Looper.prepare();
			Toast.makeText(context, text, Toast.LENGTH_SHORT).show();
			Looper.loop();
		}
	}

	/**
	 * 得到json文件中的内容
	 *
	 * @param context
	 * @param fileName
	 * @return
	 */
	public static String getJson(Context context, String fileName) {
		StringBuilder stringBuilder = new StringBuilder();
		//获得assets资源管理器
		AssetManager assetManager = context.getAssets();
		//使用IO流读取json文件内容
		try {
			BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(
					assetManager.open(fileName), "utf-8"));
			String line;
			while ((line = bufferedReader.readLine()) != null) {
				stringBuilder.append(line);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return stringBuilder.toString();
	}
	/**
	 * 获取两个日期之间的间隔时间
	 * 还剩多少年/月/天/小时/分钟
	 * @return
	 */
	public static String getGapCount(Date startDate, Date endDate) {
		long minute = 60 * 1000;// 1分钟
		long hour = 60 * minute;// 1小时
		long day = 24 * hour;// 1天
		long month = 31 * day;// 月
		long year = 12 * month;// 年
		long diff = endDate.getTime() -startDate.getTime();
		long r = 0;
		if (diff > year) {
			r = (diff / year);
			return "距结束还剩"+r+"年";
		}
		if (diff > month) {
			r = (diff / month);
			return "距结束还剩"+r+"月";
		}
		if (diff > day) {
			r = (diff / day);
			return "距结束还剩"+r+"天";
		}
		if (diff > hour) {
			r = (diff / hour);
			return "距结束还剩"+r+"小时";
		}
		if (diff > minute) {
			r = (diff / minute);
			return "距结束还剩"+r+"分钟";
		}
		return "已结束";
	}

	/**
	 * 计算时间差
	 * 几分钟/小时/天前
	 *
	 * @param date
	 * @return
	 */
	public static String getTimeFormatText(Date date) {
		long minute = 60 * 1000;// 1分钟
		long hour = 60 * minute;// 1小时
		long day = 24 * hour;// 1天
		long month = 31 * day;// 月
		long year = 12 * month;// 年

		if (date == null) {
			return null;
		}
		long diff = new Date().getTime() - date.getTime();
		long r = 0;
		if (diff > year) {
			r = (diff / year);
			return r + "年前";
		}
		if (diff > month) {
			r = (diff / month);
			return r + "个月前";
		}
		if (diff > day) {
			r = (diff / day);
			return r + "天前";
		}
		if (diff > hour) {
			r = (diff / hour);
			return r + "小时前";
		}
		if (diff > minute) {
			r = (diff / minute);
			return r + "分钟前";
		}
		return "刚刚";
	}


	public static int getScreenWidth(Activity activity) {

		DisplayMetrics metrics = new DisplayMetrics();
		activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
		return metrics.widthPixels;
	}

	/**
	 * bitmap转file
	 * 压缩文件
	 */
	public static File compressImage(Bitmap bitmap) {
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);//质量压缩方法，这里100表示不压缩，把压缩后的数据存放到baos中
		int options = 100;
		while (baos.toByteArray().length / 1024 > 500) {  //循环判断如果压缩后图片是否大于500kb,大于继续压缩 友盟缩略图要求不大于18kb
			baos.reset();//重置baos即清空baos
			options -= 10;//每次都减少10
			bitmap.compress(Bitmap.CompressFormat.JPEG, options, baos);//这里压缩options%，把压缩后的数据存放到baos中
			long length = baos.toByteArray().length;
		}

		SimpleDateFormat format = new SimpleDateFormat("HHmmss");
		java.sql.Date date = new java.sql.Date(System.currentTimeMillis());
		//图片名
		String filename = format.format(date);

		File file = new File(Environment.getExternalStorageDirectory(), filename + ".png");
		try {
			FileOutputStream fos = new FileOutputStream(file);
			try {
				fos.write(baos.toByteArray());
				fos.flush();
				fos.close();
			} catch (IOException e) {

				e.printStackTrace();
			}
		} catch (FileNotFoundException e) {

			e.printStackTrace();
		}

		Log.d("=-=-=-=-=-", "compressImage: " + file);
		// recycleBitmap(bitmap);
		return file;
	}


	/**
	 * 获取图片名称获取图片的资源id的方法
	 *
	 * @param imageName  图片的名称，注意不用加图片的后缀
	 * @return 返回图片资源的id
	 */
	/**
	 * 获取图片名称获取图片的资源id的方法
	 *
	 * @param imageName  图片的名称，注意不用加图片的后缀
	 * @return 返回图片资源的id
	 */
	public static int getResourceId(String imageName) {
		Field field = null;
		try {
			field = R.drawable.class.getDeclaredField(imageName);
			int resId = field.getInt(field.getName());
			return resId;

		} catch (NoSuchFieldException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}

		return -1;
	}



	public static StringBuilder List_to_String(List lists){
		StringBuilder stringBuilder = new StringBuilder();
		for (Object list:lists){
			if ("".equals(stringBuilder.toString())){
				stringBuilder.append(list);
			}else{
				stringBuilder.append(",").append(list);
			}
		}
		return stringBuilder;
	}
}
